<?php

namespace app\admin\controller;

use app\admin\BaseController;
use app\admin\model\Version;
use app\Request;
use think\facade\Validate;

class VersionController extends BaseController
{

    /**版本详情
     * @fun:
     *
     * @author:崔彦雷
     * @time:2022/12/7 上午 11:02
     */
    public function versionDetail(Request $request){

        $id = $request->param('id');
        if (empty($id)){
            return $this->err_json('id不能为空');
        }
        $info = Version::where('id',$id)->find();

        return $this->suc_json('',$info);

    }

    /*
    * 版本列表
    * author xfwed
    * date 2022.07.06 11:26
    * ------------------------------------------
    * update phper  2022-03-24 07:10 将token记入缓存
   */
    public function versionList()
    {
        $all = input('param.');

        $perPage = input('param.perPage');
        $page = input('param.page');
        $version = input('param.version');//版本名称
        $version_num = input('param.version_num');//版本号
        $force_update = input('param.force_update');//是否强制更新1是 2否
        $hb_update = input('param.hb_update');//HBuilderX是否进行了更新 1是2否

        if($version){
            $where['version'] = $version;
        }
        if($version_num){
            $where['version_num'] = $version_num;
        }
        if($force_update){
            $where['force_update'] = $force_update;
        }
        if($hb_update){
            $where['hb_update'] = $hb_update;
        }
        $where = isset($where)?$where:[];
        $list = Version::where($where)
            ->order('id desc')
            ->paginate($perPage);
        return $this->suc_json('查询成功',$list);
        //return $this->ajaxReturn(0, '查询成功', $list);
    }

    /*
     * 新增版本
     * author xfwed
     * date 2022.07.06 13:44
     * ------------------------------------------
     * update phper  2022-03-24 07:10 xxxxxxxxx
    */
    public function addVersion()
    {
        $all = input('param.');
        $validate = Validate::rule([
            'version|版本名称' => 'require',
            'version_num|版本号' => 'require',
            'content|更新内容' => 'require',
            'force_update|是否强制' => 'require',
            'hb_update|HBuilderX是否更新' => 'require',
        ]);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->err_json($error,'default');
        }
        $row = Version::where('version_num',$all['version_num'])
            ->where('is_del',2)
            ->find();
        if($row){
            return $this->err_json('版本号不能重复','default');

        }

        $data['version'] = $all['version'];
        $data['version_num'] = $all['version_num'];
        $data['content'] = $all['content'];
        $data['force_update'] = $all['force_update'];
        $data['hb_update'] = $all['hb_update'];
        $data['apk_url'] = input('param.apk_url');
        $data['wgt_url'] = input('param.wgt_url');

        $add = Version::insert($data);
        if($add){
            return $this->suc_json('添加成功');
        }  else {
            return $this->err_json('添加失败','default');
        }

    }

    /*
     * 编辑版本
     * author xfwed
     * date 2022.07.06 14:22
     * ------------------------------------------
     * update phper  2022-03-24 07:10 xxxxxxxxx
    */
    public function saveVersion()
    {
        $all = input('param.');
        $validate = Validate::rule([
            'id|版本名称' => 'require',
            'version|版本名称' => 'require',
            'version_num|版本号' => 'require',
            'content|更新内容' => 'require',
            'force_update|是否强制' => 'require',
            'hb_update|HBuilderX是否更新' => 'require',
        ]);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->err_json($error,'default');
        }
        $id = $all['id'];
        $row = Version::where('version_num',$all['version_num'])
            ->where('is_del',2)
            ->where('id','<>',$id)
            ->find();
        if($row){
            return $this->err_json('版本号不能重复','default');
        }

        $data['version'] = $all['version'];
        $data['version_num'] = $all['version_num'];
        $data['content'] = $all['content'];
        $data['force_update'] = $all['force_update'];
        $data['hb_update'] = $all['hb_update'];
        $data['apk_url'] = input('param.apk_url');
        $data['wgt_url'] = input('param.wgt_url');
        $data['update_time'] = gD();

        $save = Version::where('id',$id)->update($data);
        if($save){
            return $this->suc_json('编辑成功');
        }  else {
            return $this->err_json('编辑失败','default');
        }
    }

    /*
     * 删除版本
     * author xfwed
     * date 2022.07.06 14:57
     * ------------------------------------------
     * update phper  2022-03-24 07:10 xxxxxxxxx
    */
    public function delVersion()
    {
        $all = input('param.');
        $validate = Validate::rule([
            'id|版本名称' => 'require',
        ]);
        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, 'default');
        }
        $id = $all['id'];
        $data['is_del'] = 1;
        $data['update_time'] = gD();

        $save = Version::where('id',$id)->update($data);
        if($save){
            return $this->suc_json('删除成功');
        }  else {
            return $this->err_json('删除失败', 'default');
        }
    }

}